package com.lagou.edu.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <br />
 *
 * @createTime 2020-12-15 17:39:22
 * @author dev-center
 */

@RestControllerAdvice(basePackages = {"com.lagou.edu"})
public class ExceptionConfig {

    @ExceptionHandler(value = Exception.class)
    public void errorHandlerOverJson(HttpServletResponse res, Exception ex) throws IOException {
        res.setContentType("application/json;charset=UTF-8");
      if(ex instanceof BusinessException){
          BusinessException be = (BusinessException) ex;
          res.getWriter().println(be.getErrorMsg());
      }else{
          res.getWriter().println(ex.toString());
      }
    }
}
